package com.example.freemaker_demo.tools;

import cn.hutool.core.util.StrUtil;
import cn.hutool.log.StaticLog;
import com.example.freemaker_demo.constant.ConstantUtil;

import java.util.List;

public class TemplateTool {
    public static String getKubeConfig(String cluster) {
        String kubeConfig = "";
        switch (cluster) {
            case ConstantUtil.aliyunCluster:
                kubeConfig = ConstantUtil.aliyunKubeConfig;
                break;
            case ConstantUtil.internalCluster:
                kubeConfig = ConstantUtil.internalKubeConfig;
                break;
            case ConstantUtil.gonghuiCluster:
                kubeConfig = ConstantUtil.gonghuiKubeConfig;
                break;
            case ConstantUtil.nteppCluster:
                kubeConfig = ConstantUtil.nteppKubeConfig;
                break;
            default:
                StaticLog.error("输入的Cluster有误。（输入为{})", cluster);
                System.exit(1);
        }
        return kubeConfig;
    }

    public static String getRepoAddress(String cluster) {
        String allImageName = "";
        switch (cluster) {
            case ConstantUtil.aliyunCluster:
                allImageName = "registry-vpc.cn-beijing.aliyuncs.com/softwise/";
                break;
            case ConstantUtil.internalCluster:
            case ConstantUtil.gonghuiCluster:
            case ConstantUtil.nteppCluster:
                allImageName = "registry.cn-beijing.aliyuncs.com/softwise/";
                break;
            default:
                StaticLog.error("输入的Cluster有误。（输入为{})", cluster);
                System.exit(1);
        }
        return allImageName;
    }

    public static String changeYamlDir(String path) {
        if (StrUtil.isBlank(path)) return path;
        if (path.startsWith("./")) {
            return path.replace("./", "/");
        } else if (!path.startsWith("/")) {
            return "/" + path;
        }
        return path;
    }

    public static String prepareTagsCommand(List<String> tags, boolean isEnable, boolean isTest) {
        if (tags.size() == 0) {
            if (isTest) {
                return "echo test-`date +\"%Y%m%d%H%M\"` > .tag\necho test-`date +\"%Y%m%d%H%M\"` > .tag";
            } else {
                return "echo `date +\"%Y%m%d%H%M\"` > .tag\necho `date +\"%Y%m%d%H%M\"` > .tags";
            }
        }

        StringBuilder sb = new StringBuilder();
        if (isTest) {
            sb.append("echo ");
            if (isEnable) sb.append(tags.get(0)).append("-");
            sb.append("test-`date +\"%Y%m%d%H%M\"` > .tag \n");
        } else {
            sb.append("echo ");
            if (isEnable) sb.append(tags.get(0)).append("-");
            sb.append("`date +\"%Y%m%d%H%M\"` > .tag \n");
        }
        for (int i = 0; i < tags.size(); i++) {
            if (isTest) {
                sb.append("                    echo ");
                if (isEnable) sb.append(tags.get(i)).append("-");
                sb.append("test-`date +\"%Y%m%d%H%M\"`");
            } else {
                sb.append("                    echo ");
                if (isEnable) sb.append(tags.get(i)).append("-");
                sb.append("`date +\"%Y%m%d%H%M\"`");
            }
            if (i == 0) {
                sb.append(" >");
            } else {
                sb.append(" >>");
            }
            if (i == tags.size() - 1) {
                sb.append(" .tags");
            } else {
                sb.append(" .tags \n");
            }
        }
        return sb.toString();
    }
}
